<template>
  <!--4 lx角色列表 -->

  <div class="breadcrumb">
    <Breadcrumb :aaa="aaa" :msgg="msg" :txt="txt" :path1="path1" />
  </div>
  <div class="con_container">
    <!-- 1 -->
    <div class="role">
      <el-card>
        <div class="role_top">
          <span>筛选搜索</span>
          <span>
            <el-button @click="clearName">重置</el-button>
            <el-button type="primary" @click="onSearch">查询搜索</el-button>
          </span>
        </div>
        <div>
          <el-form label-width="100px">
            <el-form-item label="角色名称">
              <el-input
                type="text"
                placeholder="请输入角色名称"
                v-model="name"
              ></el-input>
            </el-form-item>
          </el-form>
        </div>
      </el-card>
    </div>
    <!-- 2 -->
    <el-card style="margin-bottom: 20px">
      <div style="display: flex; justify-content: space-between">
        <div style="margin-top: 10px">数据列表</div>
        <div><el-button @click="onAddUser" type="primary">添加</el-button></div>
      </div>
    </el-card>
    <!-- 数据的展示 -->
    <el-table :data="tableData" style="width: 100%" :border="true">
      <el-table-column prop="id" label="编号" width="100" />
      <el-table-column prop="name" label="角色名称" width="150" />
      <el-table-column prop="description" label="描述" width="180" />
      <el-table-column prop="adminCount" label="用户数" width="180" />
      <!-- 自定义列模板 -->
      <el-table-column label="添加时间">
        <!-- slot 插槽写法  scope就是作用域插槽 ， scope.row 就是数据 -->
        <template #default="scope">
          <div>
            {{
              scope.row.createTime
                ? dayjs(scope.row.createTime).format("YYYY-MM-DD HH:mm:ss")
                : "/"
            }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="是否启用" width="100">
        <template #default="scope">
          <el-switch
            :value="scope.row.status === 1 ? true : false"
            @click="onSwitch(scope.row.id, scope.row.status)"
          />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="300" flex="right">
        <template #default="scope">
          <el-button @click="gotoAllocMenu(scope.row.id)">分配菜单</el-button>
          <el-button @click="gotoAllocResource(scope.row.id)"
            >分配资源</el-button
          >
          <el-button @click="onEdit(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页器 -->
    <el-pagination
      class="pagination"
      v-model:current-page="pagination.pageNum"
      v-model:page-size="pagination.pageSize"
      background
      :page-sizes="[1, 5, 10, 20]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pagination.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      style="margin-top: 20px"
    />

    <!-- 编辑组件 -->
    <Edit
      :visible="visible"
      :form="modalForm"
      @cancel="onCloseDialog"
      @confirm="onConfirmDialog"
    ></Edit>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import type { Ref } from "vue";
import * as api from "../../request/api";
import useTable from "../../hooks/useTables";
import dayjs from "dayjs";
import Edit from "../../components/Role/edit.vue";
import type { IRoleListItem } from "../../request/UmsRoleController/index";
import { updataStatusRole } from "../../request/UmsRoleController/index";
import { ElMessage, ElMessageBox } from "element-plus";
import router from "../../router";
import Breadcrumb from "../../components/breadcrumb.vue";
let msg = ref("角色列表");
let txt = ref("权限");
let path1 = ref("ums/admin");
let aaa = ref(0);

// 查询搜索
let name = ref("");
// 新增
let add = false; // false编辑 true 新增
// 模态框
let modalForm = ref({}) as Ref<IRoleListItem>;

let {
  tableData,
  getTableData,
  pagination,
  handleSizeChange,
  handleCurrentChange,
} = useTable(api.role.getRoleAllList, {
  // keyword: name.value  // 可以删掉 name.value 不是一个响应式数据
  // keyword: name, // name 是响应式的
  keyword: name, // name 是响应式的
});

// 弹框
let visible = ref(false);

// 点击去往分配菜单
let gotoAllocMenu = (id: number) => {
  console.log(id);
  router.push({
    path: "/ums/allocMenu",
    query: {
      id: id,
    },
  });
};
// 点击去往分配资源
// 点击去往分配菜单
let gotoAllocResource = (id: number) => {
  console.log(id);
  router.push({
    path: "/ums/allocResource",
    query: {
      id: id,
    },
  });
};
// 添加用户
let onAddUser = () => {
  let newUser = {
    name: "",
    description: "",
  } as IRoleListItem;
  modalForm.value = newUser;
  add = true;
  visible.value = true;
};
// 重置
let clearName = () => {
  name.value = "";
};
// 编辑
let onEdit = (row: IRoleListItem) => {
  // console.log(row);
  add = false;
  modalForm.value = row;
  // console.log(modalForm);
  visible.value = true;
};

// 子级emit组件传过来的事件
let onCloseDialog = () => {
  visible.value = false;
};

// 确认了输入框的内容之后
// 需要发送请求
let onConfirmDialog = (row: IRoleListItem) => {
  // console.log(row);  // row就是表单数据
  // 二次 确认
  ElMessageBox.confirm("是否确认提交!", "提示", {
    confirmButtonText: "OK",
    cancelButtonText: "Cancel",
    type: "warning",
  })
    .then(async () => {
      // 发请求
      // 去判断 是 编辑 还是添加
      if (add) {
        await api.role.addRoleList(row); // 新增
      } else {
        await api.role.roleUpdata(row); // 编辑
      }
      visible.value = false;
      ElMessage({
        type: "success",
        message: "操作成功",
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消提交",
      });
    });
};
// 切换状态
let onSwitch = (val: any, val2: any) => {
  updataStatusRole({
    id: val,
    status: (val2 - 1) * -1,
  }).then((res) => {
    if (res) {
      getTableData();
      ElMessage({
        message: "状态修改成功",
        type: "success",
      });
    }
    console.log(res);
  });
};

let onSearch = async () => {
  // console.log(name.value);
  getTableData();
  // 发起请求 ，搜索数据
  // let listData = await api.admin.getAdminList({ keyword: name.value })
};
getTableData();
</script>

<style scoped lang="scss">
.role {
  padding-top: 20px;
  background-color: #fff;
  margin-bottom: 20px;
  .role_top {
    display: flex;
    justify-content: space-between;
    margin-bottom: 10px;
  }
}
.con_container {
  padding: 10px 20px !important;
  margin-top: 20px;
}
// .btn {
//   // padding: 20px 15px 20px 15px;
//   background-color: #fff;
//   margin-bottom: 20px;
//   display: flex;
//   justify-content: space-between;
//   width: 100%;
// }
:deep .el-card .el-input {
  width: 200px !important;
}
:deep .el-button {
  font-size: 12px !important;
}
.breadcrumb {
  height: 45px;
  border-bottom: 1px solid #ebeef5;
  font-size: 20px;
}

//table表格
:deep .el-table .el-table__cell {
  text-align: center !important;
}
:deep .el-table thead {
  font-weight: bold !important;
}

//分页
.pagination {
  display: flex;
  justify-content: flex-end;
  margin-top: 15px;
}
</style>
